<script type="text/javascript">
if ($("#delivery_task_form").form().length > 1){
	console.log("帮助easyui删除隐藏的element");
	$("#delivery_task_form").remove();
}
</script>

<table id="delivery_task_list" class="easyui-datagrid"
        url="api/deliveryTaskController/getTask" 
        title="金融机构报表报送情况管理" 
        rownumbers="true" pagination="true" pagesize="20" 
	toolbar="#delivery_task_list_tbS">
	<thead>
		<tr>
			<th data-options="field:'ck',checkbox:true"><input type="checkbox" id="delivery_task_ck" name="DataGridCheckbox" /></th>
        	<th field="jgName" width="320" align="center">机构名称</th>
        	<th field="tableName" width="280" align="center" >报表名称</th>
        	<th field="taskDate" width="180" align="center"  formatter="delivery_task.formatdate">发布日期</th>
		</tr>
    </thead>
</table>
<div id="delivery_task_list_tbS">
	<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:delivery_task.add();">添加</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-cut" plain="true" onclick="javascript:delivery_task.del();">删除</a>
    <label> | 查询：</label>
    <input id="delivery_task_query_filter" class="easyui-textbox" data-options="prompt:'机构名称、报表名称模糊查询'" style="width:300px">
    <a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true"  onclick="javascript:delivery_task.search();">报送任务模糊查询</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-print" plain="true"  onclick="javascript:delivery_task.viewResult();">报送情况查询</a>
</div>

<div id="delivery_task_popup" class="easyui-dialog" style="width:800px;height:480px;padding:10px 30px"
        title="金融机构报表报送任务信息" buttons="#delivery_task_popup_buttons" data-options="modal:true,closed:true">
     <h2><label id="delivery_task_title">金融机构报表报送任务信息</label></h2>
     <form id="delivery_task_form" class="easyui-form" method="post">
	     <input type="text" name="id" style="display:none"/>
	     <table style="width:98%;border-collapse:separate;border-spacing:10px;">
     		<tr>
     			<td style="width:15%">选择报表：</td>
     			<td style="width:85%">
     				<input id="delivery_task_table_name" class="easyui-tagbox" data-options="
     					url:'api/deliveryTaskController/getTables',
     					valueField: 'value',
						textField: 'label',
						editable:false,
						multiple:true,
						limitToList:true,
		    			hasDownArrow:true,
     				"  style="width: 96%;"/>
     			</td>
     		</tr>
     		<tr>
     			<td style="width: 15%;">报送单位：</td>
     			<td style="width: 85%;">
     				<input id="delivery_task_jgId" class="easyui-tagbox" data-options="
     					url:'api/deliveryTaskController/getjrjg?pbcid='+$.op.jgId,
     					valueField: 'value',
						textField: 'label',
						editable:false,
						multiple:true,
						limitToList:true,
		    			hasDownArrow:true,
     				"   style="width: 96%;"/>
     			</td>
     		</tr>
     		<tr>
     			<td style="width: 15%;">报送单位：</td>
     			<td style="width: 85%;">
     				<input id="task_date" class="easyui-datebox" editable="false" style="width:280px;" />
     			</td>
     		</tr>
		</table>
	 </form>
</div>
<div id="delivery_task_popup_buttons">
	<a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="javascript:delivery_task.save();"
    style="width:90px">提交保存</a>
</div>

<div id="delivery_taskResult_popup" class="easyui-dialog" style="width:800px;height:480px;padding:10px 30px"
        title="金融机构报表报送情况" buttons="#delivery_taskResult_popup_buttons" data-options="modal:true,closed:true">
     <h5><label id="delivery_taskResult_title">金融机构报表报送情况</label></h5>
     <table id="delivery_taskResult_list" class="easyui-datagrid" rownumbers="true" 
			pagination="false" singleSelect="true" remoteSort="false" style="width:720px;">
	    <thead>
	        <tr>
	        	<th field="jgName" width="240" align="center" sortable="true"  sorter="delivery_task.sorterfun">金融机构</th>
	            <th field="tableName" width="230" align="center" sortable="true"  sorter="delivery_task.sorterfun">报表名称</th>
	            <th field="taskYear" width="70" align="center" sortable="true"  sorter="delivery_task.sorterfun">报送年度</th>
	            <th field="taskTerm" width="70" align="center" sortable="true"  sorter="delivery_task.sorterfun">报送期数</th>
	            <th field="status" width="70" align="center" sortable="true"  sorter="delivery_task.sorterfun">报送状态</th>
	        </tr>
	    </thead>
	</table>
</div>
<div id="delivery_taskResult_popup_buttons">
	<a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="javascript:delivery_task.refreshTaskResult();"
    style="width:120px">报送情况更新</a>
	<a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="javascript:delivery_task.closeResultPopup();"
    style="width:90px">关    闭</a>
</div>

<script type="text/javascript">
var delivery_task=delivery_task||{};
delivery_task={
		formatdate:function(val,row){
			if (val!=null)
				return moment(val).format("YYYY年MM月DD日");
			else
				return "";
		},
		sorterfun:function(a,b){
			return (a>b?1:-1);
		},
		search:function(){
			$('#delivery_task_list').datagrid("load",{filter:$("#delivery_task_query_filter").textbox("getText")});
		},
		add:function(){
			$("#delivery_task_form").form("reset");
			//弹出窗口初始化
			$("#delivery_task_table_name").combobox('setValues',[]);
			$("#delivery_task_jgId").combobox('setValues',[]);
			$("#task_date").datebox('setText',moment().format("YYYY-MM-DD"));
			$("#delivery_task_popup").dialog("open");
		},
		save:function(){
			var tables = $("#delivery_task_table_name").combobox('getValues').toString();
			if (tables == ""){
				$.messager.alert("提示","请选择报表。")
				return;
			}
			var jgs = $("#delivery_task_jgId").combobox('getValues');
			if (jgs == ""){
				$.messager.alert("提示","请选择报送单位。")
				return;
			}
			
			var tdate = $("#task_date").datebox('getText').toString().split('-');
			var taskDate = moment({ years:tdate[0], months:tdate[1] - 1, date:tdate[2]});
			
			//$.messager.alert("提示","可以保存。"+taskDate);
			
			$('#delivery_task_form').form('submit',{
				url:'api/deliveryTaskController/save',
				onSubmit:function(param){
					param.tables = tables;
					param.jgs = jgs;
					param.taskDate = taskDate;
				},
				success:function(data){
					var data = JSON.parse(data);
					console.log(data);
					$.messager.alert("提示",data.msg);
					
					$('#delivery_task_popup').dialog('close');
					$("#delivery_task_list").datagrid("reload");
				}
			});
		},
		del:function(){
			var selRows = $('#delivery_task_list').datagrid('getChecked'); 
			if (selRows.length == 0){
				$.messager.alert('提示','请勾选要删除的任务。');
				return;
			}
			$.messager.confirm('提示','确认要删除所选任务么?',function(r){
			    if (r){
			    	var ids = "";
			    	if (selRows.length == 1){
			    		ids = selRows[0].id;
			    	}else{
			    		for(var i=0; i<selRows.length - 1; i++){
			    			ids += selRows[i].id + ",";
				    	}
			    		ids += selRows[selRows.length - 1].id;
			    	}
			    	$.get('api/deliveryTaskController/del',{ids:ids},function(data){
			        	var data = JSON.parse(data);
			        	$.messager.alert("提示",data.msg);
			        	$("#delivery_task_list").datagrid("reload");
			        });
			    }
			});
		},
		viewResult:function(){
			var selRows = $('#delivery_task_list').datagrid('getChecked'); 
			if (selRows.length == 0){
				$.messager.alert('提示','请至少勾选1个任务查看报送结果。');
				return;
			}
			var popupTitle = "";
			if (selRows.length == 1){
				popupTitle = selRows[0].jgName + "【" + selRows[0].tableName + "】报送情况";
			}else{
				popupTitle = "金融机构报表报送情况";
			}
			$('#delivery_taskResult_popup').panel({title: popupTitle});
			$('#delivery_taskResult_title').html(popupTitle);
			var taskids = "";
			if (selRows.length == 1){
				taskids = selRows[0].id;
			}else{
				for (var i=0;i<selRows.length - 1;i++){
					taskids += selRows[i].id + ",";
				}
				taskids += selRows[selRows.length - 1].id;
			}
			loadData(taskids);
			$('#delivery_taskResult_popup').dialog('open');
		},
		refreshTaskResult:function(){
			var selRows = $('#delivery_task_list').datagrid('getChecked'); 
			if (selRows.length == 0){
				$.messager.alert('提示','请至少勾选1个任务查看报送结果。');
				return;
			}
			var taskids = "";
			if (selRows.length == 1){
				taskids = selRows[0].id;
			}else{
				for (var i=0;i<selRows.length - 1;i++){
					taskids += selRows[i].id + ",";
				}
				taskids += selRows[selRows.length - 1].id;
			}
			$.ajax({
				type : 'POST',
				url : 'api/deliveryTaskController/refreshTaskResult',
				dataType : 'json',
				data : {
						"taskids" : taskids
					},
				success : function(data) {
					if (data){
						if (data.code == 0){
							loadData(taskids);
						}
					}
				}
			});
		},
		closeResultPopup:function(){
			$('#delivery_taskResult_popup').dialog('close');
		}
}
function loadData(taskids){
	$("#delivery_taskResult_list").datagrid({data:[]});
	$.ajax({
		type : 'POST',
		url : 'api/deliveryTaskController/getTaskResult',
		dataType : 'json',
		data : {
				"taskids" : taskids
			},
		success : function(data) {
			if (data){
				$("#delivery_taskResult_list").datagrid({"data":data.data});
			}
		}
	});
}
</script>